using ASP;
using HoWave.Web;
using HoWave.Web.Shop;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Tuan_Tuanlist : Shop, IRequiresSessionState
{
	protected UserControls_cmpy_Top Top1;
	protected Literal Literal1;
	protected Repeater RepeatList;
	protected Label LbPage;
	protected Repeater Repeater1;
	protected UserControls_CMYP_Foot Foot1;
	protected HtmlForm form1;
	public string tag = "";
	public int flag = 0;
	public int PageSizeNum = 0;
	public string WhereStr = "";
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Init(object sender, EventArgs e)
	{
		try
		{
		}
		catch (Exception ex)
		{
			base.WriteTemErrMsg(ex.Message.ToString());
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		this.tag = base.Request.QueryString["tag"];
		if (!base.IsPostBack)
		{
			base.ShowCityArea();
			this.PopularTuanBind();
			if (this.tag != null)
			{
				if (this.tag == "Now")
				{
					this.Literal1.Text = "进行中的团购";
					this.DataBind();
				}
				else
				{
					this.Literal1.Text = "历史团购";
					this.BindHistoryData();
					this.flag = 1;
				}
			}
		}
	}
	public void WhereAddress()
	{
		SiteConfigBll siteConfigBll = new SiteConfigBll();
		if (siteConfigBll.GetSiteString("SiteConfigInfo/CityArea") != "0")
		{
			if (base.Request.Cookies["Howave_City"] != null)
			{
				string text = HttpUtility.UrlDecode(base.Request.Cookies["Howave_City"].Values["_Name"]).ToString();
				if (text != "全国")
				{
					this.WhereStr = this.WhereStr + " and ProductPlace Like '%" + text + "%'";
				}
			}
		}
	}
	public new void DataBind()
	{
		this.WhereAddress();
		CurrPageLinks currPageLinks = new CurrPageLinks();
		int num = 1;
		if (base.Request.QueryString["pageindex"] != null && base.Request.QueryString["pageindex"].ToString() != "")
		{
			num = int.Parse(base.Request.QueryString["pageindex"].ToString());
		}
		int pCount = this.conn.ExecuteCount("select count(*) from HLShop_Product where ProductType=3 and EnableSale=1 and Stocks>0 and DateDiff(s,getdate(),EndDate)>0 " + this.WhereStr);
		int num2 = (num - 1) * 10;
		DataTable dataTable = this.conn.GetDataTable(string.Concat(new object[]
		{
			"select top 10 *,BuyTimes as BuyNums from HLShop_Product where ProductID not in(select top ",
			num2,
			" ProductID from HLShop_Product where ProductType=3 and EnableSale=1 and Stocks>0 and DateDiff(s,getdate(),EndDate)>0 ",
			this.WhereStr,
			" Order by ProductID Desc) and ProductType=3 and EnableSale=1 and Stocks>0 and DateDiff(s,getdate(),EndDate)>0 ",
			this.WhereStr,
			" order by ProductID desc"
		}));
		this.PageSizeNum = dataTable.Rows.Count;
		this.RepeatList.DataSource = dataTable;
		this.RepeatList.DataBind();
		this.LbPage.Text = currPageLinks.CurrPageLink(num, 10, pCount, "个商品", "Tuanlist.aspx?tag=Now&pageindex");
	}
	public void BindHistoryData()
	{
		this.WhereAddress();
		CurrPageLinks currPageLinks = new CurrPageLinks();
		int num = 1;
		if (base.Request.QueryString["pageindex"] != null && base.Request.QueryString["pageindex"].ToString() != "")
		{
			num = int.Parse(base.Request.QueryString["pageindex"].ToString());
		}
		int pCount = this.conn.ExecuteCount("select count(*) from HLShop_Product where ProductType=3 and (Stocks<=0 or DateDiff(s,getdate(),EndDate)<=0) " + this.WhereStr);
		int num2 = (num - 1) * 10;
		DataTable dataTable = this.conn.GetDataTable(string.Concat(new object[]
		{
			"select top 10 *,case when BuyTimes>0 then BuyTimes else TeamCurrentNumber1+TeamCurrentNumber2+TeamCurrentNumber3+TeamCurrentNumber4+TeamCurrentNumber5 end as BuyNums from HLShop_Product where ProductID not in(select top ",
			num2,
			" ProductID from HLShop_Product where ProductType=3 and (Stocks<=0 or DateDiff(s,getdate(),EndDate)<=0) ",
			this.WhereStr,
			" Order by ProductID Desc) and ProductType=3 and (Stocks<=0 or DateDiff(s,getdate(),EndDate)<=0) ",
			this.WhereStr,
			" order by ProductID desc"
		}));
		this.PageSizeNum = dataTable.Rows.Count;
		this.RepeatList.DataSource = dataTable;
		this.RepeatList.DataBind();
		this.LbPage.Text = currPageLinks.CurrPageLink(num, 10, pCount, "个商品", "Tuanlist.aspx?tag=History&pageindex");
	}
	public string CountRebate(string productID)
	{
		object[] dataNum = this.GetDataNum(int.Parse(productID));
		return dataNum[1].ToString();
	}
	public string CountEconomical(string ProductID)
	{
		object[] dataNum = this.GetDataNum(int.Parse(ProductID));
		return dataNum[2].ToString();
	}
	public string TuanPrice(string ProductID)
	{
		object[] dataNum = this.GetDataNum(int.Parse(ProductID));
		return dataNum[0].ToString();
	}
	public string JustTuanSuccess(string ProductID)
	{
		object[] dataNum = this.GetDataNum(int.Parse(ProductID));
		return dataNum[3].ToString();
	}
	public object[] GetDataNum(int ProductID)
	{
		int num = 1;
		object[] array = new object[4];
		array[0] = 0;
		array[1] = 0;
		array[2] = 0;
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_Product where ProductID=" + ProductID);
		if (dataTable != null && dataTable.Rows.Count > 0)
		{
			int num2 = int.Parse(dataTable.Rows[0]["TeamNum"].ToString());
			decimal num3 = decimal.Parse(dataTable.Rows[0]["TeamPrice" + num2].ToString());
			decimal num4 = decimal.Parse(dataTable.Rows[0]["StandPrice"].ToString());
			decimal num5 = Math.Round(num3 / num4 * 10m, 2);
			decimal num6 = num4 - num3;
			array[0] = num3;
			array[1] = num5;
			array[2] = num6;
			int num7 = int.Parse(dataTable.Rows[0]["TeamNumber1"].ToString());
			int num8 = int.Parse(dataTable.Rows[0]["TeamNumber2"].ToString());
			int num9 = int.Parse(dataTable.Rows[0]["TeamNumber3"].ToString());
			int num10 = int.Parse(dataTable.Rows[0]["TeamNumber4"].ToString());
			int num11 = int.Parse(dataTable.Rows[0]["TeamNumber5"].ToString());
			int num12 = int.Parse(dataTable.Rows[0]["BuyTimes"].ToString());
			switch (num2)
			{
			case 1:
				if (num12 < num7)
				{
					num = 0;
				}
				break;
			case 2:
				if (num12 < num7 && num12 < num8)
				{
					num = 0;
				}
				break;
			case 3:
				if (num12 < num7 && num12 < num8 && num12 < num9)
				{
					num = 0;
				}
				break;
			case 4:
				if (num12 < num7 && num12 < num8 && num12 < num9 && num12 < num10)
				{
					num = 0;
				}
				break;
			case 5:
				if (num12 < num7 && num12 < num8 && num12 < num9 && num12 < num10 && num12 < num11)
				{
					num = 0;
				}
				break;
			}
			array[3] = num;
		}
		dataTable.Dispose();
		return array;
	}
	public void PopularTuanBind()
	{
		DataTable dataTable = this.conn.GetDataTable("select top 10 * from HLShop_Product  where ProductType=3 and EnableSale=1 and Stocks>0 and DateDiff(s,getdate(),EndDate)>0 and Hits>=10 order by Hits desc");
		this.Repeater1.DataSource = dataTable;
		this.Repeater1.DataBind();
	}
}
